<?php
/**
 * 
 * @author ferd
 *
 */
class AccountingDb {
	private static $_instance;
	
	private $_db_name;
	
	private function __construct(){
		$this->_db_name = variable_get("accounting_db_name","accounting");
	}
	
	public static function get_instance(){
		if(!isset(self::$_instance)){
			self::$_instance = new AccountingDb();
		}
		return self::$_instance;
	}
	
	public function get_account($acct_id){
		$acct = array();
		$sql = "select * from {accounts} where id='%d'";
		$prev_db = db_set_active($this->_db_name);
		
		$result = db_query($sql,array($acct_id));
		$acct = db_fetch_object($result);
		
		db_set_active($prev_db);
		
		return $acct;
	}
	
	public function get_accounts($match){
		$accts = array();
		if($match==""){
			return $accts;
		} else {
			$prev_db = db_set_active($this->_db_name);
			
			$sql = "select title from {accounts} where title like '%s%%'";
			$result = db_query($sql,array($match));
			$return = array();
			while($r = db_fetch_object($result)){
				$return[$r->title] = check_plain($r->title);
			}
			
			db_set_active($prev_db);
			
			return drupal_json($return);
		}
	}
}